﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using WeChatDemo.Models;
using WeChatDemo.Models.DTO;
using WeChatDemo.DataProvider;

namespace WeChatDemo.Controllers
{
    public class ArticleController : ApiController
    {
        [HttpGet]
        public IHttpActionResult Get()
        {
            try
            {
                IEnumerable<DTOArticle> _list = null;
                _list = DAOArticle.GetList("AND VideoUrl is Null ORDER BY ShowIndex");

                if (_list.Count<DTOArticle>() == 0)
                {
                    return NotFound();
                }
                else
                {
                    return Ok(_list);
                }
            }
            catch (Exception ex)
            {

                return InternalServerError(ex);
            }
            
        }

        [HttpGet]
        public IHttpActionResult Get([FromUriAttribute] string where)
        {
            try
            {
                IEnumerable<DTOArticle> _list = null;
                _list = DAOArticle.GetList(where);

                if (_list.Count<DTOArticle>() == 0)
                {
                    return NotFound();
                }
                else
                {
                    return Ok(_list);
                }
            }
            catch (Exception ex)
            {

                return InternalServerError(ex);
            }
        }

        [HttpGet]
        public IHttpActionResult Get(Guid id)
        {
            IEnumerable<DTOArticle> _list = null;
            _list = DAOArticle.GetList($"AND ArticleID = '{id}'");

            if (_list.Count<DTOArticle>() == 0)
            {
                return NotFound();
            }
            else
            {
                return Ok(_list.ElementAt<DTOArticle>(0));
            }
        }

    }
}
